package com.yjt.javatest.controller;

import com.alibaba.fastjson.JSONObject;
import com.yjt.javatest.entity.Question;
import com.yjt.javatest.service.QuestionService;
import com.yjt.javatest.utils.APIResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/question")
@Tag(name = "题目")
@PreAuthorize("@ex.isTeacher()")
public class QuestionController {
    @Resource
    QuestionService questionService;


    @PostMapping("/createQuestion")
    @Operation(summary = "新建题目")
    public APIResponse createQuestion(@RequestParam @NotNull Integer type, @RequestParam Integer score,
                                      @RequestBody @NotNull JSONObject content, @RequestParam @NotNull Integer categoryId
                                      )  {
        return questionService.createQuestion(type, score, categoryId, content);
    }

    @PostMapping("/deleteQuestion/{id}")
    @Operation(summary = "根据id删除题目",description = "删除题目将会把所有测试含有的该题目一并删除")
    public APIResponse deleteQuestion(@PathVariable Integer id) {
        return questionService.deleteQuestion(id);
    }

    @PostMapping("/{id}")
    @Operation(summary = "修改题目内容", description = "用于修改题目内容")
    public APIResponse updateQuestionContent(@PathVariable Integer id,@RequestBody JSONObject content) {
        return questionService.updateQuestion(id,content);
    }
    @PostMapping("/")
    @Operation(summary = "修改题目")
    public APIResponse updateQuestion(@RequestBody Question question) {
        return questionService.updateQuestion(question);
    }
    @PostMapping("/updateCategoryOfQuestion")
    @Operation(summary = "修改题目所属分类", description = "题目和分类属于一一对应")
    public APIResponse updateCategoryOfQuestion(@RequestParam @NotNull Integer questionId,@RequestParam @NotNull Integer newCategoryId) {
        return questionService.updateCategoryOfQuestion(questionId,newCategoryId);
    }
    @GetMapping("/{id}")
    @Operation(summary = "根据id获取题目")
    public APIResponse getQuestionById(@PathVariable Long id) {
        return APIResponse.success(questionService.getById(id));
    }
    @GetMapping("/test/{id}")
    @Operation(summary = "根据测试id获取所有题目")
    public APIResponse getQuestionsByTestId(@PathVariable Long id) {
        return questionService.getQuestionsByTestId(id);
    }

    @GetMapping("/getQuestionsByUserId")
    @Operation(summary = "获取当前用户所有题目")
    public APIResponse getQuestionsByUserId() {
        return questionService.getQuestionsByUserId();
    }

    @GetMapping("/getQuestionsByCategoryId/{categoryId}")
    @Operation(summary = "根据题目分类id获取当前用户的所有题目",description = "categoryId为0获取默认分类的所有题目")
    public APIResponse getQuestionsByCategoryId(@PathVariable Integer categoryId) {
        return questionService.getQuestionsByCategoryId(categoryId);
    }


}
